#!/bin/bash

# Vert.cj 快速性能基准测试脚本
# 用于快速验证框架性能表现

set -e

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# 配置
SERVER_PORT=8080
TEST_DURATION=10
THREADS=4
CONNECTIONS=100

echo -e "${BLUE}🚀 Vert.cj 快速性能基准测试${NC}"
echo "========================================"
echo "测试配置:"
echo "  - 端口: $SERVER_PORT"
echo "  - 持续时间: ${TEST_DURATION}秒"
echo "  - 线程数: $THREADS"
echo "  - 并发连接: $CONNECTIONS"
echo ""

# 检查依赖
check_dependencies() {
    echo -e "${YELLOW}🔍 检查测试依赖...${NC}"
    
    # 检查wrk
    if ! command -v wrk &> /dev/null; then
        echo -e "${RED}❌ wrk 未安装${NC}"
        echo "请安装wrk:"
        echo "  macOS: brew install wrk"
        echo "  Ubuntu: sudo apt-get install wrk"
        echo "  CentOS: sudo yum install wrk"
        exit 1
    fi
    
    # 检查cjpm
    if ! command -v cjpm &> /dev/null; then
        echo -e "${RED}❌ cjpm 未安装${NC}"
        echo "请安装仓颉语言和cjpm包管理器"
        exit 1
    fi
    
    echo -e "${GREEN}✅ 依赖检查通过${NC}"
}

# 构建项目
build_project() {
    echo -e "${YELLOW}🔨 构建项目...${NC}"
    
    if ! cjpm build; then
        echo -e "${RED}❌ 项目构建失败${NC}"
        exit 1
    fi
    
    echo -e "${GREEN}✅ 项目构建成功${NC}"
}

# 启动服务器
start_server() {
    echo -e "${YELLOW}🌐 启动Vert.cj服务器...${NC}"
    
    # 检查端口是否被占用
    if lsof -Pi :$SERVER_PORT -sTCP:LISTEN -t >/dev/null; then
        echo -e "${RED}❌ 端口 $SERVER_PORT 已被占用${NC}"
        echo "请停止占用端口的进程或更改测试端口"
        exit 1
    fi
    
    # 启动服务器 (后台运行)
    cjpm run --name vert.examples.final_demo > server.log 2>&1 &
    SERVER_PID=$!
    
    # 等待服务器启动
    echo "等待服务器启动..."
    sleep 3
    
    # 检查服务器是否启动成功
    for i in {1..10}; do
        if curl -s http://localhost:$SERVER_PORT/api/health > /dev/null 2>&1; then
            echo -e "${GREEN}✅ 服务器启动成功 (PID: $SERVER_PID)${NC}"
            return 0
        fi
        echo "等待服务器启动... ($i/10)"
        sleep 1
    done
    
    echo -e "${RED}❌ 服务器启动失败${NC}"
    cat server.log
    exit 1
}

# 运行性能测试
run_benchmark() {
    echo -e "${YELLOW}⚡ 开始性能测试...${NC}"
    echo ""
    
    # 基础健康检查测试
    echo "📊 测试端点: /api/health"
    echo "----------------------------------------"
    wrk -t$THREADS -c$CONNECTIONS -d${TEST_DURATION}s \
        --latency \
        http://localhost:$SERVER_PORT/api/health
    
    echo ""
    echo "📊 测试端点: /api/info"
    echo "----------------------------------------"
    wrk -t$THREADS -c$CONNECTIONS -d${TEST_DURATION}s \
        --latency \
        http://localhost:$SERVER_PORT/api/info
    
    echo ""
    echo "📊 测试端点: / (主页)"
    echo "----------------------------------------"
    wrk -t$THREADS -c$CONNECTIONS -d${TEST_DURATION}s \
        --latency \
        http://localhost:$SERVER_PORT/
}

# 清理资源
cleanup() {
    echo -e "${YELLOW}🧹 清理资源...${NC}"
    
    if [ ! -z "$SERVER_PID" ]; then
        echo "停止服务器 (PID: $SERVER_PID)"
        kill $SERVER_PID 2>/dev/null || true
        wait $SERVER_PID 2>/dev/null || true
    fi
    
    # 清理日志文件
    rm -f server.log
    
    echo -e "${GREEN}✅ 清理完成${NC}"
}

# 生成测试报告
generate_report() {
    echo ""
    echo -e "${BLUE}📋 测试总结${NC}"
    echo "========================================"
    echo "测试时间: $(date)"
    echo "测试配置: ${THREADS}线程, ${CONNECTIONS}连接, ${TEST_DURATION}秒"
    echo "服务器: Vert.cj v1.0.0"
    echo "环境: $(uname -s) $(uname -m)"
    echo ""
    echo "详细结果请查看上方测试输出"
    echo ""
    echo -e "${GREEN}🎉 性能测试完成！${NC}"
}

# 错误处理
trap cleanup EXIT

# 主执行流程
main() {
    check_dependencies
    build_project
    start_server
    run_benchmark
    generate_report
}

# 执行主函数
main "$@"
